
cd "$datadir"


use "city_level_analysis_data_ANALYSIS.dta", replace


 local controls Unemployment_rate_ HPIwith2000base 
 
 
* Figure 2
 
foreach x in cand_female  cand_white cand_hisp cand_black cand_api occ_BARF {
	
eststo `x':	reghdfe `x' post   , a( placefips year) cluster(placefips)

eststo `x'_winner:	reghdfe winner_`x' post    , a( placefips year) cluster(placefips)
}


 coefplot (   cand_female, asequation(Female) \  cand_white, asequation(Race/Eth.: White) \  cand_hisp, asequation(Race/Eth.: Hisp.) \  cand_api, asequation(Race/Eth.: API)  \  cand_black, asequation(Race/Eth.: Black) \  occ_BARF, asequation(Bus./Fin. Occ.)  ) (  cand_female_winner, asequation(Female) \  cand_white_winner, asequation(Race/Eth.: White) \  cand_hisp_winner, asequation(Race/Eth.: Hisp.) \ cand_api_winner, asequation(Race/Eth.: API)  \  cand_black_winner, asequation(Race/Eth.: Black) \    occ_BARF_winner, asequation(Bus./Fin. Occ.))    , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 
 
 graph export "$dist_output/winner_AND_cands_cand_chars.png", as(png) name("Graph") replace 
 
 
* Figure 3

foreach x in  share_cvap_white share_cvap_hisp share_renter_occ_housing    ln_pc_inc_yr     share_SNAPrec {

eststo `x':	reghdfe `x' post   , a( placefips year) cluster(placefips)
 
eststo `x'_w:	reghdfe winner_`x' post  , a( placefips year) cluster(placefips)
 
}

 
 
 coefplot (share_cvap_white, asequation(% White) \ share_cvap_hisp, asequation(% Hisp.) \ share_renter_occ_housing, asequation(% Renter) \     ln_pc_inc_yr, asequation(ln(PC Inc.))\       share_SNAPrec, asequation(% SNAP) ) (share_cvap_white_w, asequation(% White) \ share_cvap_hisp_w, asequation(% Hisp.) \ share_renter_occ_housing_w, asequation(% Renter) \    ln_pc_inc_yr_w, asequation(ln(PC Inc.))    \  share_SNAPrec_w, asequation(% SNAP) )     , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 
 
    
 graph export "$dist_output/winner_AND_cands_nhood_chars_v2.png", as(png) name("Graph") replace 
 
 
 
 * Figure 4
 
 * hisp
 reghdfe winner_cand_hisp  rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7  , a( placefips year) cluster(placefips)
 
 coefplot , keep( rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7) vertical recast(line) cirecast(rarea) omit levels(95 90)   ciopts( recast(rarea rarea) color(%40 %20)   lpattern(dot dot))  yline(0) xline(4)  name(es_hisp, replace) title( (a) Hisp. Share of Winning Candidates)
 
 * white
 reghdfe winner_cand_white  rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7 , a( placefips year) cluster(placefips)

 coefplot , keep( rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7) vertical recast(line) cirecast(rarea) omit levels(95 90)   ciopts( recast(rarea rarea) color(%40 %20)   lpattern(dot dot))  yline(0) xline(4)  name(es_white, replace)  title( (b) White Share of Winning Candidates)
 
 * renter
reghdfe winner_share_renter_occ_housing  rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7  , a( placefips year) cluster(placefips)

 coefplot , keep( rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7) vertical recast(line) cirecast(rarea) omit levels(95 90)   ciopts( recast(rarea rarea) color(%40 %20)   lpattern(dot dot))  yline(0) xline(4)   name(es_renter, replace) title( (c) Avg. N'Hood. Renter Share of Winning Cands.)
 
 * income
  reghdfe winner_ln_pc_inc_yr  rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7  , a( placefips year) cluster(placefips)

 coefplot , keep( rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7) vertical recast(line) cirecast(rarea) omit levels(95 90)   ciopts( recast(rarea rarea) color(%40 %20)   lpattern(dot dot))  yline(0) xline(4)   name(es_pc_inc, replace) title( (d) Avg. N'Hood. Per Cap. Inc. of Winning Cands.)
 
 * occupation
 reghdfe winner_occ_BARF  rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7  , a( placefips year) cluster(placefips)

coefplot , keep( rel3_1 rel3_2 rel3_3 omit  rel3_5 rel3_6 rel3_7) vertical recast(line) cirecast(rarea) omit levels(95 90)   ciopts( recast(rarea rarea) color(%40 %20)   lpattern(dot dot))  yline(0) xline(4)   name(es_BARF, replace)  title( (e) Bus./Fin. Occ. Share of Winning Cands.)

 
 graph combine es_hisp es_white     es_renter   es_pc_inc  es_BARF, cols(2) ysize(4in) xsize(5in) 
 graph export "$dist_output/event_studies_multi.png", as(png)   replace 

 
 
* Figure 5


gen renter_share=share_renter_occ_housing
gen winner_renter_share=winner_share_renter_occ_housing

foreach x in   cand_white {
	

eststo `x': reghdfe `x' postXHI_high_inc_dissim postXLO_high_inc_dissim  , a( placefips year) cluster(placefips)
*outreg2 using "$dist_output/dist_cand_chars", keep(post) word tex(frag)  excel   dec(3) nocons  label 

test postXHI_high_inc_dissim= postXLO_high_inc_dissim 

eststo `x'_winner: reghdfe winner_`x' postXHI_high_inc_dissim postXLO_high_inc_dissim  , a( placefips year) cluster(placefips)
*outreg2 using "$dist_output/dist_winner_cand_chars", keep(post) word tex(frag)  excel   dec(3) nocons   label

test postXHI_high_inc_dissim= postXLO_high_inc_dissim 

 

}


foreach x in     cand_hisp     occ_BARF renter_share ln_pc_inc_yr {
	
eststo `x':	reghdfe `x' postXHI_high_inc_dissim postXLO_high_inc_dissim   , a( placefips year) cluster(placefips)
*outreg2 using "$dist_output/dist_cand_chars", keep(post) word tex(frag) excel   dec(3) nocons  label

test postXHI_high_inc_dissim= postXLO_high_inc_dissim 


eststo `x'_winner:	reghdfe winner_`x' postXHI_high_inc_dissim postXLO_high_inc_dissim    , a( placefips year) cluster(placefips)
*outreg2 using "$dist_output/dist_winner_cand_chars", keep(post) word tex(frag)  excel   dec(3) nocons label

test postXHI_high_inc_dissim= postXLO_high_inc_dissim 


 
}

    coefplot ( cand_white, asequation(Race/Eth.: White) \  cand_hisp, asequation(Race/Eth.: Hisp.) \  occ_BARF, asequation(Bus./Fin. Occ.) \   renter_share, asequation(N'hood: % Renter) \  ln_pc_inc_yr, asequation(N'hood: ln(PC Inc.)) ) (    cand_white_winner, asequation(Race/Eth.: White) \  cand_hisp_winner, asequation(Race/Eth.: Hisp.)  \    occ_BARF_winner, asequation(Bus./Fin. Occ.)   \ renter_share_winner, asequation(N'hood: % Renter) \   ln_pc_inc_yr_winner, asequation(N'hood: ln(PC Inc.)))  , keep(postXHI_high_inc_dissim postXLO_high_inc_dissim)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners"))  
	
	 graph export "$dist_output/income_seg_split_BG_seg.png", as(png) name("Graph") replace 

	 
	 
* Fig 6


foreach x in       winner_cand_whiteXbus    winner_cand_whiteXhigh_inc   winner_cand_whiteXhigh_rent   {
eststo `x': reghdfe  `x' post , a( placefips year) cluster(placefips)
}



foreach x in       winner_cand_hispXbus    winner_cand_hispXhigh_inc   winner_cand_hispXhigh_rent    {
eststo `x': reghdfe  `x' post , a( placefips year) cluster(placefips)
}


    coefplot ( winner_cand_hispXbus, asequation(Business Occ.)  \     winner_cand_hispXhigh_inc, asequation(High Inc. N'Hood)  \   winner_cand_hispXhigh_rent, asequation(High Renter N'Hood)  )  ( winner_cand_whiteXbus, asequation(Business Occ.)  \      winner_cand_whiteXhigh_inc, asequation(High Inc. N'Hood)    \   winner_cand_whiteXhigh_rent, asequation(High Renter N'Hood)   ) , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Share of Winners with Noted Characteristic) legend( lab(3 "+ Hispanic") lab(6 "+ White")) 

   
   graph export "$dist_output/hisp_and_white_interactions_combo.png", as(png)   replace 
   
   

   
   
   
   
*** APPENDIX FIGURES ***

* Figure A1

 foreach x in  occ_BRF occ_BARF business busonly busother finance educator engineer lawyer {
	
eststo `x':	reghdfe `x' post   , a( placefips year) cluster(placefips)

eststo `x'_winner:	reghdfe winner_`x' post    , a( placefips year) cluster(placefips)
 
}


 coefplot (   occ_BARF, asequation(Bus./Fin. Occ.) \  business, asequation(Business) \      finance, asequation(Finance)  \ busonly, asequation(Small Business)  \   occ_BRF, asequation(Bus./Fin. Occ. (w/ Small Bus.)) \ educator, asequation(Educator) \  engineer, asequation(Engineer) \  lawyer, asequation(Legal)  ) (   occ_BARF_winner, asequation(Bus./Fin. Occ.) \  business_winner, asequation(Business)  \    finance_winner, asequation(Finance)  \  busonly_winner, asequation(Small Business)  \occ_BRF, asequation(Bus./Fin. Occ. (w/ Small Bus.)) \  educator_winner, asequation(Educator) \  engineer_winner, asequation(Engineer) \  lawyer_winner, asequation(Legal)  )     , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 

  graph export "$dist_output/occupation_breakdown.png", as(png) name("Graph") replace 
  
  
  * Figure A2
  
    foreach x in   highincocc workingclass {
	
eststo `x':	reghdfe `x' post   , a( placefips year) cluster(placefips)

eststo `x'_winner:	reghdfe winner_`x' post    , a( placefips year) cluster(placefips)
 
}

 coefplot (   highincocc, asequation(High Inc. Occ.) \  workingclass, asequation(Working Class Occ.)  ) (  highincocc_winner, asequation(High Inc. Occ.) \  workingclass_winner, asequation(Working Class Occ.)  )     , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 

     graph export "$dist_output/gpt_occupation_breakdown.png", as(png) name("Graph") replace 
	 
	 
* Figure A3
	 
	
 eststo clear

foreach x in cand_white  cand_hisp      {
	eststo `x':	reghdfe `x' post  if nm_cand_white==1  , a( placefips year) cluster(placefips)

	eststo `x'_w:	reghdfe winner_`x' post  if nm_cand_white==1  , a( placefips year) cluster(placefips)
}

foreach x in   occ_BARF highincocc workingclass   {
	eststo `x':	reghdfe `x' post   if nm_occ_BARF==1  , a( placefips year) cluster(placefips)

	eststo `x'_w:	reghdfe winner_`x' post  if nm_occ_BARF==1  , a( placefips year) cluster(placefips)
}

foreach x in  share_renter_occ_housing ln_pc_inc_yr {
	eststo `x':	reghdfe `x' post   if nm_occ_BARF==1  , a( placefips year) cluster(placefips)

	eststo `x'_w:	reghdfe winner_`x' post   if nm_address==1 , a( placefips year) cluster(placefips)
}

   coefplot ( cand_white, asequation(Race/Eth.: White) \  cand_hisp, asequation(Race/Eth.: Hisp.) \  occ_BARF, asequation(Bus./Fin. Occ.) \  highincocc, asequation(High Inc. Occ.) \  workingclass, asequation(Working Class Occ.)   \ share_renter_occ_housing, asequation(N'hood: % Renter) \  ln_pc_inc_yr, asequation(N'hood: ln(PC Inc.)) ) (    cand_white_w, asequation(Race/Eth.: White) \  cand_hisp_w, asequation(Race/Eth.: Hisp.)  \    occ_BARF_w, asequation(Bus./Fin. Occ.) \ highincocc_w, asequation(High Inc. Occ.) \  workingclass_w, asequation(Working Class Occ.)   \ share_renter_occ_housing_w, asequation(N'hood: % Renter) \   ln_pc_inc_yr_w, asequation(N'hood: ln(PC Inc.)))    , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 

 graph export "$dist_output/appx_main_outcomes_full_match.png", as(png) name("Graph") replace 
 
 
* Fig A4

eststo clear

foreach x in cand_white  cand_hisp occ_BARF highincocc workingclass share_renter_occ_housing ln_pc_inc_yr {
	eststo `x':	reghdfe `x' post  Unemp  HPI , a( placefips year) cluster(placefips)

	eststo `x'_w:	reghdfe winner_`x' post  Unemp  HPI, a( placefips year) cluster(placefips)
}

   coefplot ( cand_white, asequation(Race/Eth.: White) \  cand_hisp, asequation(Race/Eth.: Hisp.) \  occ_BARF, asequation(Bus./Fin. Occ.) \  highincocc, asequation(High Inc. Occ.) \  workingclass, asequation(Working Class Occ.)   \ share_renter_occ_housing, asequation(N'hood: % Renter) \  ln_pc_inc_yr, asequation(N'hood: ln(PC Inc.)) ) (    cand_white_w, asequation(Race/Eth.: White) \  cand_hisp_w, asequation(Race/Eth.: Hisp.)  \    occ_BARF_w, asequation(Bus./Fin. Occ.) \ highincocc_w, asequation(High Inc. Occ.) \  workingclass_w, asequation(Working Class Occ.)   \ share_renter_occ_housing_w, asequation(N'hood: % Renter) \   ln_pc_inc_yr_w, asequation(N'hood: ln(PC Inc.)))    , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 

 graph export "$dist_output/appx_main_outcomes_add_controls.png", as(png) name("Graph") replace 

 
* Fig A5

eststo clear

foreach x in cand_white  cand_hisp occ_BARF highincocc workingclass share_renter_occ_housing ln_pc_inc_yr {
	eststo `x':	reghdfe `x' post  [pw=ipt_wt_trim_p1], a( placefips year) cluster(placefips)

	eststo `x'_w:	reghdfe winner_`x' post [pw=ipt_wt_trim_p1], a( placefips year) cluster(placefips)
}

   coefplot ( cand_white, asequation(Race/Eth.: White) \  cand_hisp, asequation(Race/Eth.: Hisp.) \  occ_BARF, asequation(Bus./Fin. Occ.) \  highincocc, asequation(High Inc. Occ.) \  workingclass, asequation(Working Class Occ.)   \ share_renter_occ_housing, asequation(N'hood: % Renter) \  ln_pc_inc_yr, asequation(N'hood: ln(PC Inc.)) ) (    cand_white_w, asequation(Race/Eth.: White) \  cand_hisp_w, asequation(Race/Eth.: Hisp.)  \    occ_BARF_w, asequation(Bus./Fin. Occ.) \ highincocc_w, asequation(High Inc. Occ.) \  workingclass_w, asequation(Working Class Occ.)   \ share_renter_occ_housing_w, asequation(N'hood: % Renter) \   ln_pc_inc_yr_w, asequation(N'hood: ln(PC Inc.)))    , keep(post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 

 graph export "$dist_output/appx_main_outcomes_weight.png", as(png) name("Graph") replace 

 
 
* Fig A6
 
foreach x in cand_white  cand_hisp occ_BARF highincocc workingclass share_renter_occ_housing ln_pc_inc_yr {

 
eststo `x': did2s `x', first_stage(i.placefips i.year) second_stage(i.post) treatment(post) cluster(placefips)
 
eststo `x'_w: did2s winner_`x', first_stage(i.placefips i.year) second_stage(i.post) treatment(post) cluster(placefips)
 }

   coefplot ( cand_white, asequation(Race/Eth.: White) \  cand_hisp, asequation(Race/Eth.: Hisp.) \  occ_BARF, asequation(Bus./Fin. Occ.) \  highincocc, asequation(High Inc. Occ.) \  workingclass, asequation(Working Class Occ.)   \ share_renter_occ_housing, asequation(N'hood: % Renter) \  ln_pc_inc_yr, asequation(N'hood: ln(PC Inc.)) ) (    cand_white_w, asequation(Race/Eth.: White) \  cand_hisp_w, asequation(Race/Eth.: Hisp.)  \    occ_BARF_w, asequation(Bus./Fin. Occ.) \ highincocc_w, asequation(High Inc. Occ.) \  workingclass_w, asequation(Working Class Occ.)   \ share_renter_occ_housing_w, asequation(N'hood: % Renter) \   ln_pc_inc_yr_w, asequation(N'hood: ln(PC Inc.)))    , keep(1.post)  xline(0) swapnames levels(95 90) xtitle(DiD Estimates - Impact of Districts)   legend( lab(3 "All Cands.") lab(6 "Winners")) 
   
    graph export "$dist_output/appx_main_outcomes_2sdid.png", as(png) name("Graph") replace 
